import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

// 视频配置，秒
const videoDurationMin = 6;
const videoDurationMax = 900;

/// picker 选取器
class ActionPicker {
  /// 选取器 array , 多列
  static Picker array({
    required PickerAdapter adapter,
    List<int>? selected,
  }) {
    return Picker(
      /// 数据项高度
      itemExtent: 35.w,

      /// 选取器高度
      height: 250.w,

      /// 背景色
      backgroundColor: Colors.transparent,

      /// 容器颜色
      containerColor: Colors.white,

      /// 头部栏位隐藏
      hideHeader: true,

      /// 未选中文字样式
      textStyle: const TextStyle(
        fontSize: 16,
        height: 1.2,
        color: AppColors.textLevelThree,
      ),

      /// 选中文字样式
      selectedTextStyle: const TextStyle(
        fontSize: 16,
        height: 1.2,
        color: AppColors.primaryGreen,
      ),

      /// 数据适配器
      adapter: adapter,

      /// 选中项 [index]
      selecteds: selected,
    );
  }

  /// 相册 assets
  static Future<List<AssetEntity>?> assets({
    List<AssetEntity>? selected,
    RequestType type = RequestType.image,
    int maxAssets = 9,
    SpecialPickerType? specialPickerType,
    Widget? Function(BuildContext, AssetPathEntity?, int)? specialItemBuilder,
    SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
  }) async {
    var privilege = await Privilege.photos();
    if (!privilege.result) {
      CommonDialog.twoButton(
        title: privilege.title,
        description: privilege.message,
        cancelTitle: '稍后再说',
        confirmTitle: '知道了!',
        onConfirm: () => Privilege.openSettings(),
      );
      return null;
    }
    var result = await AssetPicker.pickAssets(
      Get.context!,
      pickerConfig: AssetPickerConfig(
        selectedAssets: selected,
        requestType: type,
        maxAssets: maxAssets,
        themeColor: AppColors.primaryGreen,
        specialPickerType: specialPickerType,
        filterOptions: FilterOptionGroup(
          orders: [const OrderOption(type: OrderOptionType.createDate)],
          videoOption: const FilterOption(
            durationConstraint: DurationConstraint(
              min: Duration(seconds: videoDurationMin),
              max: Duration(seconds: videoDurationMax),
            ),
          ),
        ),
        specialItemPosition: specialItemPosition,
        specialItemBuilder: specialItemBuilder,
      ),
    );
    return result;
  }

  /// 相机
  static Future<AssetEntity?> camera() async {
    var privilege = await Privilege.camera();
    if (!privilege.result) {
      CommonDialog.twoButton(
        title: privilege.title,
        description: privilege.message,
        cancelTitle: '稍后再说',
        confirmTitle: '知道了!',
        onConfirm: () => Privilege.openSettings(),
      );
      return null;
    }
    var result = await CameraPicker.pickFromCamera(Get.context!);
    return result;
  }
}
